<template>
	<view class="cu-modal bottom-modal" :class="{'show':isShow}" @click="hide">
		<view class="cu-dialog">
			<view class="goods-data">
				<view class="thumb">
					<image :src="skuImage" mode="">
					</image>
				</view>
				<view class="item">
					<view class="title">
						<text></text>
					</view>
					<view class="price">
						<text class="min">￥</text>
						<text class="max">{{price}}</text>
						<text class="min">.00</text>
					</view>
					<view class="inventory">
						<text>库存：{{stock}}</text>
					</view>
				</view>
			</view>
			<view class="attr-size">
				<view class="attr-list" v-for="(item,index) in AttrSizeList" :key="index">
					<view class="title">
						<text>{{item.attr}}</text>x
					</view>
					<view class="size-list">
						<div class="list" v-for="(value,idx) in item.SizeList"
							:class="{'action':AttrSizeList[index].index === idx}"
							@click.stop="onAttrSize(item,value,index,idx)" :key="idx">

							<text>{{value.size}}</text>
						</div>
					</view>
				</view>
				<view class="attr-number" @click.stop="onStop">
					<view class="tit">数量</view>
					<view class="number">
						<text class="iconfont icon-jian" @click="sub"></text>
						<input type="tel" v-model="num" maxlength="8">
						<text class="iconfont icon-jia" @click="add"></text>
					</view>
				</view>
			</view>

			<view class="attr-btn">
				<view class="add-cart" v-if="BuyType === 1" @click="onConfirm(BuyType)">加入购物车</view>
				<view class="add-buy" v-if="BuyType === 1" @click="onConfirm(BuyType)">立即购买</view>
				<view class="confirm" v-if="BuyType === 2 || BuyType === 3" @click="onConfirm(BuyType)">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
    //method 1 秒杀  2 拼团  3砍价
		props: ['attrs', 'image', 'sku', 'method','config'],
		data() {
			return {
				isShow: false,
				AttrIndex: 0,
				SizeIndex: 0,
				AttrSizeList: [],
				// 购买类型
				BuyType: 0,
				temp: [],
				price: '1599',
				stock: "",
				skuImage: "",
				skuId: "",
				num: 1
			};
		},
		methods: {
			add() {
				var num = this.num
				num++
				this.num = num
			},
			sub() {
				var num = this.num
				num--
				if (num == 0) {
					num = 1
				}
				this.num = num
			},
			/**
			 * 显示 
			 * @param {Number} type 1 点击选择 2 加入购物 3 立即购买
			 */
			show(type) {
				if (this.method == 1) {
					var skus = this.sku
					for (let var1 in skus) {
						this.price = skus[var1]['sku_price']
						this.stock = skus[var1]['sku_stock']
						this.skuImage = skus[var1]['sku_image']
						this.skuId = skus[var1]['id']
					}
				}

				this.skuImage = this.image
				var attrs = this.attrs
				var AttrSizeList = []
				for (let attr of attrs) {
					var attr_value = attr.attr_value
					var SizeList = []
					for (let i = 0; i < attr_value.length; i++) {
						var data = {
							index: i,
							size: attr_value[i],
							attr: attr.attr_name
						}
						SizeList.push(data)
					}
					var info = {
						index: 0,
						attr: attr.attr_name,
						SizeList
					}
					AttrSizeList.push(info)
				}
				this.AttrSizeList = AttrSizeList
				this.BuyType = type;
				this.isShow = true;
			},
			hide() {
				this.isShow = false;
			},
			onStop() {

			},
			/**
			 * 属性选择点击
			 */
			onAttrSize(item, value, index, idx) {
				if (this.method == 1) {
					uni.showToast({
						title: "已选择,请购买"
					})
				} else {
					this.AttrSizeList[index].index = idx;
					this.AttrIndex = item.index;
					this.SizeIndex = value.index;
					var data = this.temp
					data[value.attr] = value.size
					var str = ''
					var arr = []
					for (let var1 in data) {
						arr.push(data[var1])
					}
					arr.sort()
					for (let s of arr) {
						str += s + "|"
					}
					str = str.slice(0, str.length - 1)
					var skus = this.sku
					for (let var2 in skus) {
						if (str == var2) {
							this.price = skus[var2]['sku_price']
							this.stock = skus[var2]['sku_stock']
							this.skuImage = skus[var2]['sku_image']
							this.skuId = skus[var2]['id']
						}
					}
				}
			},
			/**
			 * 确认点击
			 */
			onConfirm(type) {
				if (type == 3) {
					if(this.method == 1){
						var skuId = this.skuId
						var totalPrice = this.price * this.num
						this.$http.get('seckill/action',{
							seckill_id:this.config,
							sku_id:skuId
						},{}).then(res => {
							console.log(res)
							if(res.data.code == 400){
								uni.showModal({
									title:res.data.message,
									complete: () => {
										uni.navigateBack({
											delta:1
										})
									}
								})
							}
						})
						uni.navigateTo({
							url: '/pages/Order/Order?skuIds=' + skuId + "&totalPrice=" + totalPrice + "&num=" + this
								.num+ "&order_type_code="+3+"&seckill_id="+this.config
						})
					}
					return
				}
				var skuId = this.skuId
				var cartNum = this.num
				this.$http.post('cart', {
					sku_id: skuId,
					cart_num: cartNum
				}, {}).then(res => {
					console.log(res)
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'GoodsAttr.scss';
</style>
